package LearnAlgorithm.e_多维数组and矩阵;

import java.util.Scanner;

public class j矩阵运算加减乘 {
	public static int[][] A = null;
	public static int[][] B = null;
	public static void main(String[] args) {
		long before = System.currentTimeMillis();
//		matrixAdditionAndSubtraction(A, B);
		util.Util.duration(before);
		A = new int[][] {
			{1,0,3,-1},
			{2,1,0,2},
		};
		B = new int[][] {
			{4,1,0},
			{-1,1,3},
			{2,0,1},
			{1,3,4},
		};
		long before2 = System.currentTimeMillis();
		matrixmultiplication(A, B);
		util.Util.duration(before);
	}
	
	
	
	/**
	 * O(N^2)
	 * @param A
	 * @param B
	 */
	public static void matrixAdditionAndSubtraction(int[][] A, int[][] B) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("开始录入row");
		int row = scanner.nextInt();
		System.out.println("开始录入col");
		int col = scanner.nextInt();
		System.out.println("开始录入符号");
		String symbol = scanner.next();
		A = new int[row][col];
		B = new int[row][col];
		System.out.println("开始录入矩阵A");
		for (int currentRow = 0; currentRow < row; currentRow++) {
			for (int currentCol = 0; currentCol < col; currentCol++) {
				A[currentRow][currentCol] = scanner.nextInt();
			}
		}
		System.out.println("开始录入矩阵B");
		for (int currentRow = 0; currentRow < row; currentRow++) {
			for (int currentCol = 0; currentCol < col; currentCol++) {
				B[currentRow][currentCol] = scanner.nextInt();
			}
		}
		int[][] answer = new int[row][col];
		switch (symbol.charAt(0)) {
		case '+':
			for (int currentRow = 0; currentRow < row; currentRow++) {
				for (int currentCol = 0; currentCol < col; currentCol++) {
					answer[currentRow][currentCol] = A[currentRow][currentCol] + B[currentRow][currentCol];
				}
			}
			break;
		case '-':
			for (int currentRow = 0; currentRow < row; currentRow++) {
				for (int currentCol = 0; currentCol < col; currentCol++) {
					answer[currentRow][currentCol] = A[currentRow][currentCol] - B[currentRow][currentCol];
				}
			}
			break;
		default:
			break;
		}
		printMatrix(A, row, col);
		System.out.println();
		printMatrix(B, row, col);
		System.out.println();
		printMatrix(answer, row, col);
		System.out.println();
	}
	
	/**
	 * O(N^3)
	 * @param A
	 * @param B
	 */
	public static void matrixmultiplication(int[][] A, int[][] B) {
		int Arow = A.length;
		int Acol = A[0].length;
		int Brow = B.length;
		int Bcol = B[0].length;
		if (Arow == 0 || Acol == 0 || Brow == 0 || Bcol == 0) {
			System.out.println("输入了空矩阵");
			System.exit(-1);
		} else if (Acol != Brow) {
			System.out.println("输入了非合法两矩阵");
			System.exit(-1);
		}
		int[][] answer = new int[Arow][Bcol];
		for (int currentRow = 0; currentRow < Arow; currentRow++) {
			for (int currentCol = 0; currentCol < Bcol; currentCol++) {
				for (int current = 0; current < Acol; current++) {//这里三重循环，将来温习的自己，慢慢看，你肯定能看懂
					answer[currentRow][currentCol] += A[currentRow][current] * B[current][currentCol];
				}
			}
		}
		/*
		answer = {
			{9,-2,-1},
			{9,9,11},
		}
		 */
		printMatrix(A, Arow, Acol);
		System.out.println();
		printMatrix(B, Brow, Bcol);
		System.out.println();
		printMatrix(answer, Arow, Bcol);
		System.out.println();
	}
	
	/**
	 * 打印二维数组
	 * @param matrix
	 * @param length
	 */
	public static void printMatrix(int[][] matrix, int row, int col) {
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				System.out.print(matrix[i][j] + "\t");
			}
			System.out.println();
		}
	}
}
